package easy;

public class Day12 {
    public static void main(String[] args) {
        ListNode l1 = new ListNode(1);
        l1.next = new ListNode(2);
        l1.next.next = new ListNode(4);

        ListNode l2 = new ListNode(1);
        l2.next = new ListNode(3);
        l2.next.next = new ListNode(4);

        ListNode listNode = mergeTwoLists(l1, l2);
        while (listNode != null) {
            System.out.print(listNode.val + "\t");
            listNode = listNode.next;
        }
        System.out.println();
    }

    public static ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        ListNode listNode = new ListNode(-1);
        ListNode tempList = listNode;
        while (l1 != null || l2 != null) {
            if (l1 != null && l2 != null) {
                if (l1.val > l2.val) {
                    tempList.next = new ListNode(l2.val);
                    l2 = l2.next;
                } else {
                    tempList.next = new ListNode(l1.val);
                    l1 = l1.next;
                }
                tempList = tempList.next;
                continue;
            }
            if (l1 == null) {
                tempList.next = l2;
                break;
            }
            if (l2 == null) {
                tempList.next = l1;
                break;
            }
        }

        return listNode.next;


    }

    public static class ListNode {
        int val;
        ListNode next;

        ListNode(int x) {
            val = x;
        }
    }
}

